<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\PositionList;
use App\Models\PositionField;
use App\Models\Position;
use App\Models\News\News;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form as FormF;
use App\Models\Artists\JoinArtist;
use Illuminate\Support\Facades\Storage;

class PositionListController extends Controller
{
    protected $name = '内容列表';
    use ModelForm;
    
    public function index()
    {
		//同步头像
		/*
		$arr = PositionList::all();
		foreach($arr as $row)
		{
			dd($row->content);
			if(isset($row->content['thumb']) && !empty($row->content['thumb']))
			{
				
				$thumb = str_replace('titlepic/', '', $row->content['thumb']);
				$val = $row->content;
				$val['thumb'] = $thumb;
				dd($val);

			}
		}
		*/
		
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid().view('admin::tools.select_news'));
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
	/*
	//选择该新闻
	public function select_news()
	{
		$data = request()->all();
		if($data['id'] > 0)
		{
			$info = News::find($data['id']);
			$form = new FormF();
			$form->text('title', '标题')->default($info->title);
			$form->text('url', '链接')->default($info->titleurl);
			$form->image('thumb', '图片');
			$form->hidden('thumb_default')->default($info->titlepic);
			$form->html('<img src="'.$info->titlepic.'" width="300" />', '当前图片');
			$form->text('desc', '简介')->default($info->description);
		}else {
			$form = new FormF();
			$form->text('title', '标题');
			$form->text('url', '链接');
			$form->image('thumb', '图片');
			$form->text('desc', '简介');
		}
		
		return ['status'=>1,'content'=> $form->render()];
	}*/
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
		$info = Position::find(request()->get('position_id'));
		
		//自动
		if($info->pattern == 2)
		{
			return $this->auto_grid($info);
			
		}else {
			//手动
			return Admin::grid(PositionList::class, function (Grid $grid) {
				$grid->model()->orderBy('listorder', 'asc')->orderBy('id', 'DESC');
				
				$grid->filter(function($filter){
					
					$filter->like('title', '标题');
					$filter->equal('position_id', '位置ID');
				
				});
				$grid->disableCreateButton();
				$grid->tools(function ($tools) {
					
					$tools->append('<button class="btn btn-default" onclick="show_right()">搜索新增</button>');
					$tools->append('<a href="/admin/positionlist/create?position_id='.request()->get('position_id').'" class="btn btn-default" >新增</a>');
				});
				
				$grid->actions(function ($action) {
					$row = $action->row;
					$action->prepend('<a href="/admin/positionlist/'.$row->id.'/edit?position_id='.$row->position_id.'" title="编辑" class="gird-edit-new"><i class="fa fa-edit"></i></a>');
				});
				
				$grid->column('id', 'ID');
				$grid->column('listorder', '排序')->editable()->sortable();
				
				$grid->column('content', '标题')->display(function($content){
					return $content["title"];
				});
				
			});
		}
        
    }
	
	//自动
	protected function auto_grid($info)
	{
		return Admin::grid(News::class, function (Grid $grid) use ($info) {
			
			$date = Position::selectTimes();
			$grid->model()->whereDate('created_at', '>', $date[$info->auto_setting['time_range']]);
			$grid->model()->where('class_id', $info->auto_setting['class_id']);
			if($info->auto_setting['titlepic'] == 1)
			{
				$grid->model()->whereNotNull('titlepic');
			}
			$grid->model()->orderBy(Position::$selectOrders[$info->auto_setting['order']], 'DESC');
			
			$grid->filter(function($filter){
				
				$filter->like('index_title', '标题');
			
			});
			$grid->actions(function ($actions) {
				$actions->disableDelete();
				$actions->disableEdit();
				$url = news_url($actions->row->created_at, $actions->row->id, true);
				$actions->append('<a href="'.$url.'" title="预览" target="_blank"><i class="fa fa-eye"></i></a>');
			});
			$grid->disableCreateButton();
			
			$grid->column('id', 'ID')->sortable();
			
			$grid->column('title', '标题');
			
		});
	}
	//顶一下
	public function ding()
	{
		$id = request()->get('id');
		if($id > 0)
		{
			$info = PositionList::findOrFail($id);
			$total = count($info->self_lists);
			foreach($info->self_lists as $k=>$row)
			{
				if($row->id == $id)
				{
					if($k > 0)
					{
						
					}else{
						break;
					}
				}
			}
		}
	}
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(PositionList::class, function (Form $form) {
			//编辑备注
            $form->display('id', '备注')->with(function(){
				return '<p style="line-height: 35px;">'.Position::find($this->position_id)->remark.'</p>';
			});
			//新增备注
			if(request()->get('position_id')>0)
			{
				$form->html('<p style="line-height: 35px;">'.Position::find(request()->get('position_id'))->remark.'</p>', '备注');
			}
			//去除右上角列表
			$form->tools(function (Form\Tools $tools) {
				// 去掉跳转列表按钮
				$tools->disableListButton();
			});
						
			
            $form->number('listorder', '排序');
			$form->hidden('position_id')->default(request()->get('position_id'));
			
			
			$form->embeds('content', '内容', function ($form) {
				$news_id = request()->get('new_id',0);
				$artist_id = request()->get('artist_id',0);
				if($news_id > 0)
				{
					$info = News::find($news_id);
				}elseif($artist_id>0)
				{
					$info = JoinArtist::find($artist_id);
					
				}
				if(isset($info) && !empty($info))
				{
					$form->text('title', '标题')->default($info->title);
					$form->text('url', '链接')->default($info->titleurl);
					$form->image('thumb', '图片')->uniqueName();
					
					$form->text('desc', '简介');
				}else{
					$form->text('title', '标题');
					$form->text('url', '链接');
					$form->image('thumb', '图片')->uniqueName();
					$form->text('desc', '简介');
				}
				$fields = PositionField::where('position_id', request()->get('position_id'))->get();
				if(!$fields->isEmpty())
				{
					foreach($fields as $field)
					{
						if(in_array($field->field, ['title', 'thumb', 'url', 'desc']))continue;
						if(strstr($field->field, 'thumb'))
						{
							$form->image($field->field, $field->name)->uniqueName();
						}else{
							$form->text($field->field, $field->name);
						}
					}
				}
				
			});
			$form->saving(function(Form $form){
				//dd($form);
			});
			

            
        });
    }
}
